﻿using Dapper;
using Domain.Entity.In;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class Instock_dRepository : IInstock_dRepository
    {

        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";

        //添加入库明细
        public async Task<int> AddInstock_d(Instock_d instock_D)
        {
            using(SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"insert into[dbo].[in_instock_d](in_id, slot_id, proc_id, proc_name, slot_name, ind_numtotal, ind_price, ind_num, ind_instockOK_num, ind_instockNO_num, ind_total, proc_date, remark)
                                values({instock_D.in_id},{instock_D.slot_id},{instock_D.proc_id},'{instock_D.proc_name}','{instock_D.slot_name}','{instock_D.ind_numtotal}','{instock_D.ind_price}',{instock_D.ind_num},{instock_D.ind_instockOK_num},{instock_D.ind_instockNO_num},'{instock_D.ind_total}','{instock_D.proc_date}','{instock_D.remark}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        //删除入库明细
        public async Task<int> DeleteInstock_d(int indid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"delete from [dbo].[in_instock_d] where ind_id={indid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //根据入库单id查询明细
        public async Task<List<Instock_d>> GetInstock_Ds(int instockid)
        {
            using(SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"select * from [dbo].[in_instock_d] where in_id={instockid}";
                return sqlConnection.Query<Instock_d>(sql).ToList();
            }
        }


        //修改入库明细
        public async Task<int> UpdateInstock_d(Instock_d instock_D)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[in_instock_d] set in_id={instock_D.in_id},slot_id={instock_D.slot_id},slot_name='{instock_D.slot_name}',proc_id={instock_D.proc_id},proc_name='{instock_D.proc_name}',ind_price='{instock_D.ind_price}',
                                ind_num={instock_D.ind_num},ind_instockOK_num={instock_D.ind_instockOK_num},ind_instockNO_num={instock_D.ind_instockNO_num},ind_total='{instock_D.ind_total}',proc_date='{instock_D.proc_date}',remark='{instock_D.remark}' where ind_id={instock_D.ind_id}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
